import functools


class Solution(object):
    def getKth(self, lo, hi, k):

        @functools.cache
        def cal(val):
            if val == 1:
                return 0
            if val % 2 == 0:
                return 1 + cal(val // 2)
            else:
                return 1 + cal(3 * val + 1)
        out = [[cal(val), val] for val in range(lo, hi + 1)]
        out.sort()
        return out[k - 1][1]
